package com.weipai.education.service.impl;

import com.weipai.education.dao.ProductCateDao;
import com.weipai.education.dao.ProductDao;
import com.weipai.education.enums.RetCode;
import com.weipai.education.model.ProductCate;
import com.weipai.education.model.api.PageResult;
import com.weipai.education.model.api.Response;
import com.weipai.education.service.ProductCateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author: 曹朋飞
 * @Date: 2018/12/11 11:23
 */
@Service
public class ProductCateServiceImpl implements ProductCateService {
    
    @Autowired
    private ProductCateDao productCateDao;
    @Autowired
    private ProductDao productDao;

    @Override
    public Response list(Integer page, Integer size) {
        Response response = new Response();
        Integer start = (page-1) * size;
        List<Map<String, Object>> product = productCateDao.list(start, size);
        Integer totalNum = productCateDao.listAmount();
        PageResult<Map<String, Object>> pageList = new PageResult<>(product,page,size,totalNum);
        response.setCode(RetCode.SUCCESS);
        response.put("pageList", pageList);
        return response;
    }

    @Override
    public Response save(ProductCate productCate) {
        Response response = new Response();
        if(null !=productCate.getId()){
            productCateDao.update(productCate);
        }else{
            productCate.setStatus(0);
            productCateDao.insert(productCate);
        }
        response.setCode(RetCode.SUCCESS);
        return response;
    }

    @Override
    public Response changeStatus(String id) {
        Response response = new Response();
        ProductCate productCate = productCateDao.selectByPrimaryKey(id);
        if(null!=productCate){
            if(0 ==productCate.getStatus()){
                
                //更改停用，首先查看是否还用此类的产品，如果有不能停用
                Integer amount = productDao.listAmount(null, id);
                if(amount>0){
                    response.setCode(RetCode.CATE_HAVE_PRODUCT_CHANGE);
                    return response;
                }
                
                productCate.setStatus(-1);
            }else{
                productCate.setStatus(0);
            }
            productCateDao.update(productCate);
            response.setCode(RetCode.SUCCESS);
        }else{
            response.setCode(RetCode.NOT_DATA);
        }
        return response;
    }

    @Override
    public Response batchDeleteById(String[] id) {
        Response response = new Response();

        //产品类别删除，如果有商品不能删除
        for(int i=0;i<=id.length-1;i++){
            Integer amount = productDao.listAmount(null, id[i]);
            if(amount>0){
                response.setCode(RetCode.CATE_HAVE_PRODUCT_DELETE);
                return response;
            }
        }
        
        if(productCateDao.batchDeleteById(id)>0){
            response.setCode(RetCode.SUCCESS);
        }else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }
}
